import re


def get_food_time(body_text):
    sents = re.split('[.!\n]+', body_text)
    check_sents = []
    for s in sents:
        s = s.strip()
        if s: check_sents.append(s)
    
    name_checker = re.compile('(monday|tuesday|wednesday|thursday|friday|saturday|sunday|today|tonight|tomorrow [evening]?|this evening)', re.I)
    range_checker = re.compile('\d*:?\d*\s*(?:pm|am)?\s*(?:\-|to)?\s*\d*:?\d+\s*(?:pm|am)')
    date_max = 0
    
    bingo = ''
    glob_name = ''
    glob_time = ''
    glob_range = ''
    
    time_keys = ['Time','When']
    time_re = "(?:"
    for t in time_keys:
        time_re += "(?<=^%s:)|(?<=^%s :)|" % (t,t)
    if time_re[-1]=='|': time_re=time_re[:-1]
    time_re += ").*"  
    
    for s in check_sents:
        #may be we're lucky
        #we don't want to match Date: because it's often in forwarded emails, as part of the header.
        #I gots no energy to identify parts of headers.
        time_match = re.search(time_re, s, re.I)
        
        name_match = name_checker.search(s)
        range_match = range_checker.search(s)
   
        if name_match and range_match:
            bingo = name_match.group(0).strip() + ' ' + range_match.group(0).strip() 
            
        if time_match and name_match:
            bingo = time_match.group(0).strip()
            break
            
        if time_match: glob_time = time_match.group(0).strip()
        if name_match and glob_name=='': glob_name = name_match.group(0).strip()        #prefer first occurance
        if range_match and glob_range=='': glob_range = range_match.group(0).strip()     #prefer first occurance
        
        #print "matches (%s,%s)" % (glob_name, glob_range)
               
    #put these together
    
    if bingo: return bingo
    if glob_time.find(glob_name)>0: glob_name=''
    if glob_time.find(glob_range)>0: glob_range=''
    if glob_name and glob_range: return glob_name + ' ' +glob_range   
    if glob_name and glob_time: return glob_name + ' ' + glob_time
    if glob_time: return glob_time
    if glob_name: return glob_name
    return glob_range
        
        
        
        
            